package com.kaidi.dibankecloud.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.kaidi.dibankecloud.R;
import com.kaidi.dibankecloud.activity.MainActivity;
import com.kaidi.dibankecloud.activity.RegisterActivity;
import com.kaidi.dibankecloud.databinding.PopupRegSuccessBinding;

/**
 * Created by Administrator on 2017/1/10.
 */

public class RegSuccessPopupWindow extends PopupWindow implements View.OnClickListener {
    private Context context;
    private View anchor;

    public RegSuccessPopupWindow(Context context) {
        super(context);
        this.context = context;
        PopupRegSuccessBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.popup_reg_success, null, false);
        binding.activatedNowBtn.setOnClickListener(this);
        binding.loginNowBtn.setOnClickListener(this);
        SpannableString string = new SpannableString(context.getString(R.string.reg_success_msg));
        string.setSpan(new AbsoluteSizeSpan(13,true), 41, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        string.setSpan(new ForegroundColorSpan(Color.BLACK),41,45, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        binding.regSuccessMsgTv.setText(string);
        setContentView(binding.getRoot());
        setWidth((int) context.getResources().getDimension(R.dimen.protocol_width));
        setHeight((int) context.getResources().getDimension(R.dimen.reg_success_height));
        ColorDrawable dw = new ColorDrawable(0000000000);
        setBackgroundDrawable(dw);
        setOutsideTouchable(true);
        setFocusable(true);
        update();
    }

    public void showPopupWindow(View anchor){
        this.anchor = anchor;
        if(!isShowing()){
            showAtLocation(anchor, Gravity.CENTER,0,0);
            Window window = ((Activity) context).getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 0.5f;
            window.setAttributes(lp);
        }
    }

    @Override
    public void onClick(View view) {
        dismiss();
        switch (view.getId()) {
            case R.id.activated_now_btn:
                ActivatedNowPopupWindow activatedNowPopupWindow = new ActivatedNowPopupWindow(context);
                activatedNowPopupWindow.setOnDismissListener((RegisterActivity)context);
                activatedNowPopupWindow.showPopupWindow(anchor);
                break;
            case R.id.login_now_btn:
                context.startActivity(new Intent(context, MainActivity.class));
                ((RegisterActivity) context).finish();
                break;
        }
    }
}
